package utils

import (
	"fmt"

	"github.com/go-resty/resty/v2"
)

// 客户端
var httpClient = resty.New()

func init() {
	httpClient.SetDebug(false)
}

func PostBody(url string, body interface{}) string {

	res, err := httpClient.R().SetBody(body).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// 带头部提交
func PostBodyHeader(url string, body interface{}, headers map[string]string) string {
	res, err := httpClient.R().SetHeaders(headers).SetBody(body).Post(url)

	if err != nil {
		panic(err)
	}

	return res.String()
}

// 带文件提交
func PostBodyFile(url string, body interface{}, fileKey, filePath string) string {
	res, err := httpClient.R().SetFile(fileKey, filePath).SetBody(body).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// 带文件提交 带header参数
func PostBodyFileHeade(url string, body interface{}, fileKey, filePath string, headers map[string]string) string {
	res, err := httpClient.R().SetHeaders(headers).SetFile(fileKey, filePath).SetBody(body).Post(url)
	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// form表单提交
func PostForm(url string, body map[string]string) string {
	res, err := httpClient.R().SetFormData(body).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// form表单提交
func PostFormHeader(url string, body map[string]string, headers map[string]string) string {
	res, err := httpClient.R().SetFormData(body).SetHeaders(headers).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// 带文件的form提交
func PostFormFile(url string, body map[string]string, fileKey, filePath string) string {
	res, err := httpClient.R().SetFile(fileKey, filePath).SetFormData(body).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// 带文件的form提交
func PostFormFileHeader(url string, body map[string]string, headers map[string]string, fileKey, filePath string) string {
	res, err := httpClient.R().SetFile(fileKey, filePath).SetHeaders(headers).SetFormData(body).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// 带文件的form提交
func PostFormFilesHeader(url string, body map[string]string, headers map[string]string, files map[string]string) string {
	res, err := httpClient.R().SetFiles(files).SetHeaders(headers).SetFormData(body).Post(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// query方式的GET请求
func GetQuery(url string, params map[string]string) string {
	res, err := httpClient.R().SetQueryParams(params).Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// query方式的GET请求 带header
func GetQueryHeaders(url string, params map[string]string, headers map[string]string) string {
	res, err := httpClient.R().SetHeaders(headers).SetQueryParams(params).Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}
	return res.String()
}

// body方式的GET请求 带header
func GetBodyHeaders(url string, params map[string]string, headers map[string]string) string {
	res, err := httpClient.R().SetHeaders(headers).SetBody(params).Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}
	return res.String()
}

// path方式的GET请求
func GetPath(url string, params map[string]string) string {
	res, err := httpClient.R().SetPathParams(params).Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// path方式的GET请求
func GetPathHeader(url string, params map[string]string, headers map[string]string) string {
	res, err := httpClient.R().SetPathParams(params).SetHeaders(headers).Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// GET请求
func Get(url string) string {
	res, err := httpClient.R().Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// GET请求
func GetHeader(url string, headers map[string]string) string {
	res, err := httpClient.R().SetHeaders(headers).Get(url)

	if err != nil {
		fmt.Println("Error", err)
	}

	return res.String()
}

// 请求头
func JsonHeader(headers map[string]string) map[string]string {
	jsonHeader := map[string]string{
		"Content-Type": "application/json",
	}
	if headers == nil {
		return jsonHeader
	}
	merge := MergeStringMap(jsonHeader, headers)
	return merge
}

// 上传二进制文件
func PutFileHeader(url string, body []byte, headers map[string]string) string {
	res, err := httpClient.R().SetBody(body).SetHeaders(headers).Put(url)
	if err == nil {
		return res.String()
	}
	return ""
}
